package com.hl.secondHand.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hl.secondHand.common.lang.Result;
import com.hl.secondHand.common.vo.EasyOrder;
import com.hl.secondHand.entity.Orders;
import com.hl.secondHand.entity.User;
import com.hl.secondHand.mapper.OrdersMapper;
import com.hl.secondHand.service.IOrdersService;
import com.hl.secondHand.service.IUserService;
import com.hl.secondHand.shiro.AccountProFile;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hl
 * @since 2021-06-08
 */
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    IOrdersService ordersService;
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    IUserService userService;

    public void addOrder(int gid,String remarks){
        int nowId = ((AccountProFile) SecurityUtils.getSubject().getPrincipal()).getId();
        User user = userService.getOne(new QueryWrapper<User>().eq("id", nowId));
        Orders orders = new Orders();
        orders.setGid(gid);
        orders.setSid(nowId);
        orders.setAddress(user.getAddress());
        orders.setRemarks(remarks);
        ordersService.saveOrUpdate(orders);
    }

    @RequiresAuthentication
    @RequestMapping("/orderList")
    public Result orderList(){
        int uid = ((AccountProFile) SecurityUtils.getSubject().getPrincipal()).getId();
        List<EasyOrder> order = ordersMapper.getEasyOrder(uid);
        for(EasyOrder torder : order){
            torder.setImage(torder.getImage()==null?"":torder.getImage().split("/;")[0]);
        }
        return Result.succes(order);
    }
}
